// let state = {
// 	verifyEmail: "",
// 	info: ""
// }
import fetch from 'isomorphic-fetch'
import {
	pushInQueue
} from '../message/action.js'

const verifyEmailSuccess = (data) => {
	return {
		type: "VERIFY_EMAIL_SUCCESS",
		info: data
	}
}

const registerSuccess = (data) => {
	return {
		type: "REGISTER_SUCCESS",
		info: data
	}
}

export const verifyEmail = (data) => {
	return dispatch => fetch(__API__.postVerifyEmail, {
		method: 'POST',
		credentials: "include",
		body: JSON.stringify(data)
	}).then(data => data.json()).then(data => {
		console.log(data)
		if (data.error) {
			dispatch(pushInQueue({
				info: data.error,
				flag: "hint"
			}))

		} else {
			dispatch(verifyEmailSuccess(data))
			dispatch(pushInQueue({
				info: "Send email success",
				flag: "success"
			}))

		}
	}).catch(err => {
		if (err) throw err
		dispatch(pushInQueue({
			info: "Internet error",
			flag: "warning"
		}))

	})
}

export const register = (data) => {
	console.log(data)
	return dispatch => fetch(__API__.postRegister, {
		method: 'POST',
		credentials: "include",
		body: JSON.stringify(data)
	}).then(data => data.json()).then(data => {
		console.log(data)
		if (data.error) {
			dispatch(pushInQueue({
				info: data.error,
				flag: "hint"
			}))
		} else {
			dispatch(registerSuccess(data))
			dispatch(pushInQueue({
				info: "Register success",
				flag: "success"
			}))
			window.location.href = "http://www.brandf.cn/"
		}
	}).catch(err => {
		if (err) throw err
		dispatch(pushInQueue({
			info: "Internet error",
			flag: "warning"
		}))

	})
}